﻿using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;
using Autodesk.Revit.DB;

namespace YJKMenuAssist.Controls
{
	public partial class RightTabControl : UserControl
	{
		public event RoutedEventHandler ClickItem;

		public TableInfos CmdTableInfos
		{
			get
			{
				return this.m_TableInfos;
			}
			set
			{
				this.m_TableInfos = value;
			}
		}

		public RightTabControl()
		{
			this.InitializeComponent();
		}

		public void Initialize_GeneraType(ICollection<Element> collection, string typeName)
		{
			this.m_DicLstGroupType.Clear();
			this.rightWrapPanel.Children.Clear();
			if (typeName == "墙")
			{
				this.GroupTypes(collection, typeName);
				return;
			}
			this.GroupTypes(collection, typeName);
		}

		private void GroupTypes(ICollection<Element> collection, string typeName)
		{
			if (typeName == "墙")
			{
				foreach (Element element in collection)
				{
					WallType wallType = (WallType)element;
					if (!this.m_DicLstGroupType.ContainsKey(wallType.FamilyName))
					{
						this.m_DicLstGroupType.Add(wallType.FamilyName, new List<Element>
						{
							wallType
						});
					}
					else
					{
						this.m_DicLstGroupType[wallType.FamilyName].Add(wallType);
					}
				}
				using (Dictionary<string, List<Element>>.KeyCollection.Enumerator enumerator2 = this.m_DicLstGroupType.Keys.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						string text = enumerator2.Current;
						this.CreateLable(text);
						foreach (Element element2 in this.m_DicLstGroupType[text])
						{
							WallType wallType2 = (WallType)element2;
							this.CreateLable(wallType2.Name, new List<string>
							{
								"绘制墙体",
								wallType2.Name,
								wallType2.FamilyName
							});
						}
					}
					return;
				}
			}
			foreach (Element element3 in collection)
			{
				FamilySymbol familySymbol = (FamilySymbol)element3;
				if (!this.m_DicLstGroupType.ContainsKey(familySymbol.FamilyName))
				{
					this.m_DicLstGroupType.Add(familySymbol.FamilyName, new List<Element>
					{
						familySymbol
					});
				}
				else
				{
					this.m_DicLstGroupType[familySymbol.FamilyName].Add(familySymbol);
				}
			}
			this.DoorAndWinGroupTypes(typeName);
		}

		private void DoorAndWinGroupTypes(string typeName)
		{
			foreach (string text in this.m_DicLstGroupType.Keys)
			{
				this.CreateLable(text);
				foreach (Element element in this.m_DicLstGroupType[text])
				{
					FamilySymbol familySymbol = (FamilySymbol)element;
					if (typeName == "门")
					{
						this.CreateLable(familySymbol.Name, new List<string>
						{
							"插入门",
							familySymbol.Name,
							familySymbol.FamilyName
						});
					}
					else
					{
						this.CreateLable(familySymbol.Name, new List<string>
						{
							"插入窗",
							familySymbol.Name,
							familySymbol.FamilyName
						});
					}
				}
			}
		}

		private void CreateLable(string sName)
		{
			Label label = new Label();
			label.Content = sName;
			label.Cursor = Cursors.Hand;
			label.HorizontalAlignment = HorizontalAlignment.Stretch;
			label.Margin = new Thickness(0.0, 5.0, 0.0, 0.0);
			new ToolTip().Content = sName;
            label.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(112, 112, 112, 112));
			this.rightWrapPanel.Children.Add(label);
		}

		private void CreateLable(string sName, List<string> tag)
		{
            SolidColorBrush background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue));
			if (sName == "分割线")
			{
                System.Windows.Shapes.Rectangle element = this.CreateSplitItem();
				this.rightWrapPanel.Children.Add(element);
				return;
			}
			Label label = new Label();
			label.Content = sName;
			label.Cursor = Cursors.Hand;
			label.HorizontalAlignment = HorizontalAlignment.Stretch;
			label.Margin = new Thickness(0.0, 5.0, 0.0, 0.0);
			label.MouseDown += new MouseButtonEventHandler(this.lblItem_Click);
			label.ToolTip = new ToolTip
			{
				Content = sName
			};
			label.Tag = tag;
			label.Background = background;
			this.rightWrapPanel.Children.Add(label);
		}

		private Rectangle CreateSplitItem()
		{
			return new Rectangle
			{
				StrokeDashArray = new DoubleCollection
				{
					1.0,
					1.0
				},
				Width = 101.0,
				Stroke = Brushes.Black,
				StrokeThickness = 2.0,
                Fill = new SolidColorBrush(System.Windows.Media.Color.FromArgb(100, 170, 170, 170)),
				Margin = new Thickness(0.0, 5.0, 0.0, 0.0),
				VerticalAlignment = VerticalAlignment.Stretch,
				HorizontalAlignment = HorizontalAlignment.Left
			};
		}

		public void Initialize()
		{
			this.rightWrapPanel.Children.Clear();
			foreach (CommandInfos commandInfos in this.m_TableInfos.CommandInfoList)
			{
				this.CreateLable(commandInfos.CmdName, new List<string>());
			}
		}

		private void SetLeftButtonBackGroundToWrite()
		{
			try
			{
                SolidColorBrush background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue));
				foreach (object obj in this.rightWrapPanel.Children)
				{
					StackPanel stackPanel = (StackPanel)obj;
					if (stackPanel != null)
					{
						for (int i = 0; i < stackPanel.Children.Count; i++)
						{
							Label label = stackPanel.Children[i] as Label;
							if (label != null)
							{
								label.Background = background;
							}
						}
					}
				}
			}
			catch
			{
			}
		}

		private void lblItem_Click(object sender, RoutedEventArgs e)
		{
			if (this.ClickItem != null)
			{
				this.SetLeftButtonBackGroundToWrite();
				this.ClickItem((Label)sender, e);
			}
		}

		private TableInfos m_TableInfos;

		private Dictionary<string, List<Element>> m_DicLstGroupType = new Dictionary<string, List<Element>>();
	}
}
